﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sharp.Portal.Other;

namespace Sharp.Portal.AdminLTE
{
    /// <summary>
    /// 工具栏代码具体实现
    /// </summary>
    public class BSToolButtons : IToolButton
    {
        /// <summary>
        /// List页
        /// </summary>
        public dynamic ListPage { get; set; }

        /// <summary>
        /// 详情按钮
        /// </summary>
        /// <param name="config"></param>
        /// <returns></returns>
        public string DetailsButton(ToolButtonInfo config)
        {
            string output;

            if (config.HasWindow)
            {
                output = "ajaxModal";
            }
            else
            {
                output = "ajaxLoad";
            }

            config.Icon = "ToolBarIco.Details";
            if (config.Direct == null)
            {
                config.OnClientClick = string.Format(
                    "window.app.showEditWindow('\" + Url.Action(\"{0}\") + \"',this,{{[参数]}}, event);",
                    string.Join("\",\"", config.UrlAction.Split(',')));

                AddFieldProperty(config, "IsReadOnlyForm", true);
            }
            if (string.IsNullOrEmpty(ListPage.ItemPanelUrlAction))
            {
                ListPage.ItemPanelUrlAction = config.UrlAction;
            }

            return CustomButton(config) + $".Attribute(\"data-toggle\", \"{output}\").Attribute(\"data-IsReadOnlyForm\", \"true\").Attribute(\"data-search\", \"#{ListPage.ListControlId}\").Attribute(\"role\",\"detailsbutton\")";
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <returns></returns>
        public string CreateButton(ToolButtonInfo config)
        {
            string output;

            if (config.HasWindow)
            {
                output = "ajaxModal";
            }
            else
            {
                output = "ajaxLoad";
            }

            config.Icon = "ToolBarIco.Create";
            if (config.Direct == null)
            {
                config.OnClientClick = string.Format(
                    "window.app.showAddWindow('\" + Url.Action(\"{0}\") + \"',this,{{[参数]}}, event);",
                    string.Join("\",\"", config.UrlAction.Split(',')));

            }
            if (string.IsNullOrEmpty(ListPage.ItemPanelUrlAction))
            {
                ListPage.ItemPanelUrlAction = config.UrlAction;
            }

            return CustomButton(config) + $".Attribute(\"data-toggle\", \"{output}\").Attribute(\"data-search\", \"#{ListPage.ListControlId}\").Attribute(\"role\",\"createbutton\")";
        }


        /// <summary>
        /// 编辑按钮
        /// </summary>
        /// <param name="config"></param>
        /// <returns></returns>
        public string EditButton(ToolButtonInfo config)
        {
            string output;

            if (config.HasWindow)
            {
                output = "ajaxModal";
            }
            else
            {
                output = "ajaxLoad";
            }


            config.Icon = "ToolBarIco.Edit";
            if (config.Direct == null)
            {
                config.OnClientClick = string.Format(
                    "window.app.showEditWindow('\" + Url.Action(\"{0}\") + \"',this,{{[参数]}},event);",
                    string.Join("\",\"", config.UrlAction.Split(',')));

            }
            if (string.IsNullOrEmpty(ListPage.ItemPanelUrlAction))
            {
                ListPage.ItemPanelUrlAction = config.UrlAction;
            }

            return CustomButton(config) + $".Attribute(\"data-toggle\", \"{output}\").Attribute(\"data-search\", \"#{ListPage.ListControlId}\").Attribute(\"role\",\"editbutton\")";
        }
        /// <summary>
        /// 删除按钮
        /// </summary>
        /// <param name="config"></param>
        /// <returns></returns>
        public string DeleteButton(ToolButtonInfo config)
        {
            config.Icon = "ToolBarIco.Delete";
            if (config.Direct == null)
            {
                config.OnClientClick = string.Format(
                    "window.app.deleteItem('{1}','\" + Url.Action(\"{0}\") + \"',this);",
                    string.Join("\",\"", config.UrlAction.Split(',')), ListPage.ListControlId);

            }


            return CustomButton(config) + $".Attribute(\"data-search\", \"#{ListPage.ListControlId}\").Attribute(\"role\",\"deletebutton\")";
        }
        /// <summary>
        /// 刷新按钮
        /// </summary>
        /// <param name="config"></param>
        /// <returns></returns>
        public string RefreshButton(ToolButtonInfo config)
        {
            config.Icon = "ToolBarIco.Refresh";
            if (config.Direct == null)
            {
                config.OnClientClick = "window.app.search(this);";

            }


            return CustomButton(config) + $".Attribute(\"data-search\", \"#{ListPage.ListControlId}\").Attribute(\"role\",\"refreshbutton\")";
        }
        /// <summary>
        /// 自定义按钮
        /// </summary>
        /// <param name="config"></param>
        /// <returns></returns>
        public string CustomButton(ToolButtonInfo config)
        {
            StringBuilder sb = new StringBuilder("@Html.Bootstrap()");
            sb.AppendFormat(".LinkButton(\"{0}\"", config.Title);
            if (config.Icon != null && config.Icon.Contains("."))
            {
                sb.AppendFormat("{0}", "," + config.Icon);
            }
            else
            {
                sb.AppendFormat("{0}", config.Icon.IsNotNullOrEmpty() ? ",\"" + config.Icon + "\"" : "");
            }


            sb.Append(")");



            if (!config.OnClientClick.IsNullOrEmpty())
            {
                sb.AppendFormat(".Click(\"{0}\")", config.OnClientClick);

                //客户端参数只支持js、固定字符串，不支持服务端代码
                if (config.Parameters != null && config.Parameters.Any())
                {
                    foreach (var p in config.Parameters)
                    {
                        var pm = p.Mode;
                        if (pm == 1)//ParameterMode.Raw
                        {
                            config.ClientClickExtraParams[p.Name] = p.Value;
                        }
                        else if (pm == 2)//借用Auto作服务器代码的传递
                        {
                            config.ClientClickExtraParams[p.Name] = string.Format("'\" + {0} + \"'", p.Value);
                        }
                        else
                        {
                            config.ClientClickExtraParams[p.Name] = string.Format("'{0}'", p.Value);
                        }

                    }
                }

                sb.Replace("[参数]",
                    string.Join(",",
                        config.ClientClickExtraParams.Select(f => string.Format("{0} : {1}", f.Key, f.Value))));

                sb.AppendFormat(".Route(Url.Action(\"{0}\"))", string.Join("\",\"", config.UrlAction.Split(',')));

                return sb.ToString();
            }


            return sb.ToString();
        }



        /// <summary>
        /// 加入表单字段属性
        /// </summary>
        /// <param name="config"></param>
        /// <param name="propertyName">属性名字</param>
        /// <param name="value">布尔值属性值</param>
        private void AddFieldProperty(ToolButtonInfo config, string propertyName, Boolean value)
        {
            config.ClientClickExtraParams[propertyName] = string.Format("{0}", value.ToString().ToLower());
        }


        /// <summary>
        /// 加入表单字段属性
        /// </summary>
        /// <param name="config"></param>
        /// <param name="propertyName">属性名字</param>
        /// <param name="value">字符串属性值</param>
        /// <param name="entryString">实体字符串，不加''</param>
        private void AddFieldProperty(ToolButtonInfo config, string propertyName, string value,
            Boolean entryString = false)
        {
            if (!config.ClientClickExtraParams.ContainsKey(propertyName))
                config.ClientClickExtraParams.Add(propertyName, string.Empty);
            if (entryString || propertyName == "regex")
                config.ClientClickExtraParams[propertyName] = string.Format("'{0}'", value);
            else
            {
                config.ClientClickExtraParams[propertyName] = value.ToJson();
            }
        }
    }
}